package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BmsAbnormalVO;
import com.tbit.uqbike.object.pojo.BmsBatch;
import com.tbit.uqbike.object.pojo.BmsBatteryHealthAnalyse;
import com.tbit.uqbike.object.pojo.dto.*;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.pojo.vo.BmsBatteryPowerChangeLogVO;
import com.tbit.uqbike.object.pojo.vo.BmsBatteryVO;
import com.tbit.uqbike.object.pojo.vo.BmsConfigVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BmsService;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 云BMS管理
 *
 * @Author:BUGTian
 * @DATE: 2022/7/13
 */
@RestController
@RequestMapping("/bms")
public class BmsController {

    @Autowired
    private BmsService bmsService;

    /**
     * 云bms配置
     *
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_EDIT_CONFIG)
    @PostMapping("/editBmsConfig")
    public Result editBmsConfig(@RequestBody BmsConfigDTO bmsConfigDTO, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (!UserConstant.USER_ADMIN.equals(login.getType())) {
            return Result.error("只允许超级管理员配置！");
        }
        return bmsService.editBmsConfig(bmsConfigDTO);
    }

    /**
     * 云bms配置查询
     *
     * @param accountId 品牌id
     * @return
     */
    @GetMapping("/queryBmsConfig")
    public Result<BmsConfigVO> queryBmsConfig(@NotNull Integer accountId) {
        if (accountId == null) {
            return Result.error("品牌id不能为空");
        }
        return Result.success(bmsService.queryBmsConfig(accountId));
    }

    /**
     * 云bms电池批次添加
     *
     * @param bmsBatchDTO
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_ADD_BATCH)
    @PostMapping("/addBmsBatch")
    public Result addBmsBatch(@RequestBody BmsBatchDTO bmsBatchDTO) {
        checkBmsBatchDTO(bmsBatchDTO);
        if (StrUtil.isNotEmpty(bmsBatchDTO.getBatchName()) && bmsBatchDTO.getBatchName().length() > 50){
            return Result.error("批次名称过长");
        }else if (StrUtil.isNotEmpty(bmsBatchDTO.getRemark()) && bmsBatchDTO.getRemark().length() > 255){
            return Result.error("备注过长");
        }
        return bmsService.addBmsBatch(bmsBatchDTO);
    }

    /**
     * 云bms电池批次修改
     *
     * @param bmsBatchUpdateDTO
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_UPDATE_BATCH)
    @PutMapping("/updateBmsBatch")
    public Result updateBmsBatch(@RequestBody BmsBatchUpdateDTO bmsBatchUpdateDTO) {
        if (StrUtil.isNotEmpty(bmsBatchUpdateDTO.getBatchName()) && bmsBatchUpdateDTO.getBatchName().length() > 50){
            return Result.error("批次名称过长");
        }else if (StrUtil.isNotEmpty(bmsBatchUpdateDTO.getRemark()) && bmsBatchUpdateDTO.getRemark().length() > 255){
            return Result.error("备注过长");
        }
        return bmsService.updateBmsBatch(bmsBatchUpdateDTO);
    }

    /**
     * 云bms电池批次删除
     *
     * @param batch 批次号
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_DELETE_BATCH)
    @DeleteMapping("/deleteBmsBatch")
    public Result deleteBmsBatch(@NotNull Integer batch) {
        return bmsService.deleteBmsBatch(batch);
    }

    /**
     * 云bms电池批次分页查询
     *
     * @param page      当前页码
     * @param size      每页显示的行
     * @param accountId 品牌id
     * @param batchName 批次名称
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/queryPageBmsBatch")
    public Result<PageResult<BmsBatch>> queryPageBmsBatch(@NotNull Integer page, @NotNull Integer size, @NotNull Integer accountId, String batchName, Date startTime, Date endTime) {
        return bmsService.queryPageBmsBatch(page, size, accountId, batchName, startTime, endTime);
    }


    /**
     * 添加云bms电池
     *
     * @param bmsBatteryDTO
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_ADD)
    @PostMapping("/addBmsBattery")
    public Result<BatchOperationMsgVO<String>> addBmsBattery(@RequestBody BmsBatteryDTO bmsBatteryDTO) {
        return bmsService.addBmsBattery(bmsBatteryDTO);
    }

    /**
     * 批量修改云bms电池的批次
     *
     * @param bmsBatteryDTO
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_UPDATE)
    @PutMapping("/updateBmsBattery")
    public Result<BatchOperationMsgVO<String>> updateBmsBattery(@RequestBody BmsBatteryDTO bmsBatteryDTO) {
        return bmsService.updateBmsBattery(bmsBatteryDTO);
    }

    /**
     * 分页查询bms电池信息
     *
     * @param bmsBatteryQueryDTO
     * @return
     */
    @GetMapping("/queryPageBmsBattery")
    public Result<PageResult<BmsBatteryVO>> queryPageBmsBattery(BmsBatteryQueryDTO bmsBatteryQueryDTO) {
        PageResult<BmsBatteryVO> pageResult = bmsService.queryPageBmsBattery(bmsBatteryQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * bms电池信息导出
     *
     * @param bmsBatteryQueryDTO
     * @return
     */
    @GetMapping("/exportBmsBattery")
    public Result exportBmsBattery(BmsBatteryQueryDTO bmsBatteryQueryDTO) {
        bmsBatteryQueryDTO.setPage(1);
        bmsBatteryQueryDTO.setSize(65535);
        PageResult<BmsBatteryVO> pageResult = bmsService.queryPageBmsBattery(bmsBatteryQueryDTO);
        List<BmsBatteryVO> list = pageResult.getList();
        ExcelUtil<BmsBatteryVO> excel = new ExcelUtil<>(BmsBatteryVO.class);
        JsonResult jsonResult = excel.exportExcel(list, "Bms电池信息");
        return Result.success(jsonResult.getData());
    }

    /**
     * 批量删除bms电池
     *
     * @param bmsBatteryNoList bms电池编号列表  多个用,隔开
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_DELETE)
    @DeleteMapping("/deleteBmsBattery")
    public Result<BatchOperationMsgVO<String>> deleteBmsBattery(String bmsBatteryNoList) {
        BatchOperationMsgVO<String> batchOperationMsgVO = bmsService.deleteBmsBattery(Arrays.asList(bmsBatteryNoList.split(",")));
        return Result.success(batchOperationMsgVO);
    }

    /**
     * bms电池图表分析
     *
     * @param bmsBatteryQueryDTO
     * @return
     */
    @GetMapping("/queryBmsBatteryHealthAnalyze")
    public Result<BmsBatteryHealthAnalyse> queryBmsBatteryHealthAnalyze(BmsBatteryQueryDTO bmsBatteryQueryDTO) {
        BmsBatteryHealthAnalyse bmsBatteryHealthAnalyse = bmsService.queryBmsBatteryHealthAnalyze(bmsBatteryQueryDTO);
        return Result.success(bmsBatteryHealthAnalyse);
    }

    /**
     * 分页查询bms电池异常分析信息
     *
     * @param page         当前页码
     * @param size         每页显示的行
     * @param accountId    品牌id
     * @param adAccountId  区域id
     * @param batteryNO    电池编号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param handle       处理状态
     * @param abnormalType 异常类型
     * @return
     */
    @GetMapping("/queryPageBmsAbnormal")
    public Result<PageResult<BmsAbnormalVO>> queryPageBmsAbnormal(@NotNull Integer page, @NotNull Integer size, @NotNull Integer accountId, @NotNull Integer adAccountId, String batteryNO, Date startTime, Date endTime, Integer handle, Integer abnormalType) {
        PageResult<BmsAbnormalVO> pageResult = bmsService.queryPageBmsAbnormal(page, size, accountId, adAccountId, batteryNO, startTime, endTime, handle, abnormalType);
        return Result.success(pageResult);
    }

    /**
     * 清除bms电池异常
     *
     * @param batteryNO    电池编号
     * @param abnormalType 异常类型
     * @param addTime      添加时间
     * @return
     */
    @OperateLog(module = ModuleEnum.BMS, methods = MethodEnum.BMS_ABNORMAL_DELETE)
    @DeleteMapping("/deleteBmsAbnormal")
    public Result deleteBmsAbnormal(@NotNull String batteryNO, @NotNull Integer abnormalType, @NotNull Date addTime) {
        bmsService.deleteBmsAbnormal(batteryNO, abnormalType, addTime);
        return Result.success();
    }

    /**
     * 导出bms电池异常分析信息
     *
     * @param accountId    品牌id
     * @param adAccountId  区域id
     * @param batteryNO    电池编号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param handle       处理状态
     * @param abnormalType 异常类型
     * @return
     */
    @GetMapping("/exportBmsAbnormal")
    public Result exportBmsAbnormal(@NotNull Integer accountId, @NotNull Integer adAccountId, String batteryNO, Date startTime, Date endTime, Integer handle, Integer abnormalType) {
        PageResult<BmsAbnormalVO> pageResult = bmsService.queryPageBmsAbnormal(1, 65535, accountId, adAccountId, batteryNO, startTime, endTime, handle, abnormalType);
        ExcelUtil<BmsAbnormalVO> excelUtil = new ExcelUtil<>(BmsAbnormalVO.class);
        return Result.success(excelUtil.exportExcel(pageResult.getList(), "Bms电池异常分析").getData());
    }

    /**
     * 分页查询云bms电池历史
     *
     * @param page        当前页码
     * @param size        每页显示的行
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param batteryNO   电池编号
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return
     */
    @GetMapping("/queryBmsBatteryPowerChangeLog")
    public Result<PageResult<BmsBatteryPowerChangeLogVO>> queryBmsBatteryPowerChangeLog(@NotNull Integer page, @NotNull Integer size, @NotNull Integer accountId, @NotNull Integer adAccountId, String batteryNO, Date startTime, Date endTime) {
        PageResult<BmsBatteryPowerChangeLogVO> pageResult = bmsService.queryBmsBatteryPowerChangeLog(page, size, accountId, adAccountId, batteryNO, startTime, endTime);
        return Result.success(pageResult);
    }

    /**
     * 导出云bms电池历史
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param batteryNO   电池编号
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return
     */
    @GetMapping("/exportBmsBatteryPowerChangeLog")
    public Result exportBmsBatteryPowerChangeLog(@NotNull Integer accountId, @NotNull Integer adAccountId, String batteryNO, Date startTime, Date endTime) {
        PageResult<BmsBatteryPowerChangeLogVO> pageResult = bmsService.queryBmsBatteryPowerChangeLog(1, 65535, accountId, adAccountId, batteryNO, startTime, endTime);
        ExcelUtil<BmsBatteryPowerChangeLogVO> excelUtil = new ExcelUtil<>(BmsBatteryPowerChangeLogVO.class);
        return Result.success(excelUtil.exportExcel(pageResult.getList(), "云bms电池历史").getData());
    }


    /**
     * 添加电池批次参数校验
     *
     * @param bmsBatchDTO
     */
    private void checkBmsBatchDTO(@RequestBody BmsBatchDTO bmsBatchDTO) {
        if (bmsBatchDTO.getAccountId() == null) {
            throw new BaseException("品牌id不能为空");
        }
        if (StringUtils.isEmpty(bmsBatchDTO.getBatchName())) {
            throw new BaseException("批次名称不能为空");
        }
        if (bmsBatchDTO.getBatchTime() == null) {
            throw new BaseException("批次日期不能为空");
        }
    }


}
